<template>
  <ModalFormTemplate
    :open="open"
    :formConfig="formConfig"
    :title="title"
    :width="width"
    @cancel="onCancel"
    @finish="onFinish"
    ref="modalRef"
  />
</template>
<script setup lang="ts">
  import { computed, ref, watch } from 'vue';
  import ModalFormTemplate from '@/components/template/form/modal-form-template.vue';

  const title = '商户资料修改'; //弹窗标题
  const width = 550; //弹窗宽度
  const modalRef = ref(); //通用模态弹窗组件引用，用与调用模态弹窗组件内部方法
  const open = ref(false); //控制弹窗显示、隐藏
  const emits = defineEmits<{ dataReload: [] }>(); //通知调用组件(父组件) 执行 父组件 的 @dataReload 的方法
  const editId = ref(0); //要修改的记录ID

  defineExpose({ show }); //暴露接口 show(显示弹窗) 方法，供 父组件 调用

  /* [关闭弹窗] 1. 重置表单 2. 关闭弹窗 */
  function onCancel() {
    modalRef.value.resetForm();
    open.value = false;
  }

  /* [显示弹窗] 1. 显示弹窗  */
  function show(id: number) {
    editId.value = id;
    open.value = true;
  }

  /* [监听弹窗显示] open = true 时，调用 getDataInfo 获取数据 */
  watch(
    () => open.value,
    (val) => {
      if (val === true) {
        getDataInfo();
      }
    }
  );

  /* [获取修改数据] 调用子组件 getDataInfo 方法详情 */
  function getDataInfo() {
    modalRef.value?.getDataInfo(getAction, getParams.value);
  }
  const getAction = 'base.merchant.mchpayconfig.ServiceImpl.get'; //get接口 方法名
  const getParams = computed(() => ({ mch_pay_config_id: editId.value })); //get接口 参数

  /* [提交弹窗] 1. 调用子组件 提交方法 2. 提交成功后，关闭弹窗 3. 刷新父组件数据 */
  async function onFinish() {
    const params = modalRef.value?.getFormParams();
    const mergedParams = { ...getParams.value, ...params }; //合并提交参数
    try {
      const ret = await modalRef.value?.postFormData(updateAction, mergedParams);
      if (ret === true) {
        onCancel();
        emits('dataReload');
      }
    } catch (error) {}
  }
  const updateAction = 'base.merchant.mchpayconfig.ServiceImpl.update'; //update接口 方法名
  /* [表单配置] */
  const formConfig = computed(() => [
    {
      title: '所属商户ID',
      dataIndex: 'mch_id',
      type: 'select' as const,
      required: true,
      props: {
        options: {
          action: 'base.merchant.mch.ServiceImpl.listSelectByMchId',
          params: {},
        },
      },
    },
    {
      title: '支付配置号',
      dataIndex: 'pay_config_no',
      type: 'input' as const,
      required: true,
      defaultValue: '',
      maxlength: 13,
    },
    {
      title: '支付配置名称',
      dataIndex: 'pay_config_name',
      type: 'input' as const,
      maxlength: 50,
      required: true,
      defaultValue: '',
    },
    {
      title: '支付配置状态',
      dataIndex: 'pay_config_status',
      type: 'switch' as const,
      props: {
        checkedChildren: '启用',
        checkedValue: 1,
        unCheckedChildren: '停用',
        unCheckedValue: 2,
      },
      required: true,
      defaultValue: 1,
    },
    {
      title: '是否设置为默认支付配置',
      dataIndex: 'default_status',
      type: 'switch' as const,
      required: true,
      props: {
        checkedChildren: '是',
        checkedValue: 1,
        unCheckedChildren: '否',
        unCheckedValue: 2,
      },
      defaultValue: 1,
    },
    {
      title: '签名方式',
      dataIndex: 'sign_type',
      type: 'select' as const,
      required: true,
      defaultValue: '',
      props: {
        options: [
          { label: 'RSA2', value: 1 },
          { label: 'MD5', value: 2 },
          { label: 'SM3', value: 3 },
        ],
      },
    },
    {
      title: '签名密钥',
      dataIndex: 'sign_secret',
      type: 'input' as const,
      maxlength: 200,
      required: true,
      defaultValue: '',
    },
    {
      title: '微信小程序跳转类型',
      dataIndex: 'wechat_jsapi_type',
      type: 'select' as const,
      required: true,
      props: {
        options: [
          { label: '服务商小程序', value: 1 },
          { label: '特约商户小程序', value: 2 },
        ],
      },
      defaultValue: '',
    },
    {
      title: '微信小程序跳转配置json',
      dataIndex: 'wechat_jsapi_params',
      type: 'input' as const,
      defaultValue: '',
    },
    {
      title: '支付宝小程序跳转类型',
      dataIndex: 'alipay_jsapi_type',
      type: 'select' as const,
      required: true,
      defaultValue: '',
      props: {
        options: [
          { label: '服务商小程序', value: 1 },
          { label: '特约商户小程序', value: 2 },
        ],
      },
    },
    {
      title: '支付宝小程序跳转配置json',
      dataIndex: 'alipay_jsapi_params',
      type: 'input' as const,
      defaultValue: '',
    },
    {
      title: '备注',
      dataIndex: 'remark',
      type: 'input' as const,
      defaultValue: '',
      maxlength: 250,
    },
  ]);
</script>
<style lang="less" scoped></style>
